
/*
 * GET home page.
 */

var mongoose = require('mongoose') ; 
//var Schema = mongoose.Schema ;
var models = require('./models') ;    // 导入自定义组件
var Cp = models.cp;
//mongoose.connect('mongodb://ucp:ch123.com@oceanic.mongohq.com:10032/cp');
mongoose.connect('mongodb://192.168.0.189:27017/cp');


//查询产品信息
exports.get = function(req,res){
    var barcode = req.param('barcode');
    Cp.findOne({sn:barcode},function(e,doc){
        if (e) res.send(e.message);
        if (doc== null) {
          res.send("0");
        }else{
          res.send(JSON.stringify(doc));
        };
    });
};
//高级搜索，按产品名称、序列号、部署地搜索
exports.findCpByPara = function(req,res){
  var pbc = req.body.bc;//barcode
  var pName = req.body.pn;//产品名称
  var pType = req.body.tp;//产品型号
  var deploySite = req.body.ds;//部署地
  Cp.find({sn: new RegExp(pbc),pn:new RegExp(pName),tp : new RegExp(pType),ds : new RegExp(deploySite)} ,function(err,docs){
     if (docs.length == 0) {
        res.send("0");
      }else{
        res.send(docs);
      };
  });
};
//验证barcode是否存在
function validationBc(barcode){
  var flag = false;
  Cp.count({bc:barcode},function(err,count){
      if(count>0){
        flag = true;
      }
  });
  return flag;
};
//保存一条产品信息
exports.saveData = function(req,res){
        var newCp = new Cp({
          sn : req.body.sn,
          pn : req.body.pn,
          cd : req.body.cd,
          ds : req.body.ds,
          bn : req.body.bn,
          tp : req.body.tp
        });
        newCp.save(function(err){
            if (err) {
                res.send("0");
            }else{
                res.send("1");
            };
        });
};
//批量保存产品信息
exports.batchSaveData = function(req,res){
  var bc1 = req.body.bc1;
  var bc2 = req.body.bc2;
  var array = new Array();
  for(var i = bc1;i<=bc2;i++){
      var newCp = new Cp({
          sn : i,
          pn : req.body.pn,
          cd : req.body.cd,
          ds : req.body.ds,
          bn : req.body.bn,
          tp : req.body.tp
        });
        array.push(newCp);
    };
  Cp.create(array,function(err,jellybean, snickers){
      if (err || jellybean == "undefined") {
          res.send("0");
        }else{
          res.send("1");
        };
  });
};
//修改产品信息
exports.updateProduct = function(req,res){
    var gpn = req.body.pn;
    var gcd = req.body.cd;
    var gds = req.body.ds;
    var gbn = req.body.bn;
    var gtp = req.body.tp;
    Cp.findByIdAndUpdate(req.body._id, {$set:{pn:gpn,cd:gcd,ds:gds,bn: gbn,tp:gtp}},function(err,cp){
        if (err) {
           res.send("0");
        }else{
            res.send(JSON.stringify(cp));
        };
    });
};
//添加维修记录
exports.saveMr = function(req,res){
    Cp.findById(req.body._id,function(err,doc){
        doc.mrs.push({md:req.body.md,me : req.body.me,con : req.body.con,mqp :req.body.mqp,mwa:req.body.mwa});
        doc.save(function(err){
            if (err) {
              res.send("0");
            }else{
              res.send("1");
            };
        });
    });
};
//删除维修记录
exports.deleteMr = function(req,res){
    Cp.findById(req.body._id,function(err,doc){
        doc.mrs.pull({_id:req.body.mid});
        doc.save(function(err){
            if (err) {
              res.send("0");
            }else{
              res.send("1");
            };
        });
    });
};

exports.index = function(req, res){
  res.render('login', { message: 'Login' });
};
//----------------------------------------------------------------------------------------------------------------------



